{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 02\n",
    "\n",
    "# 向量化内积\n",
    "Book_4《矩阵力量》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f5b0126-e220-42a5-9c76-10b5c77dd35c",
   "metadata": {},
   "source": [
    "此代码定义了两个 $2 \\times 2$ 的矩阵 $A$ 和 $B$，并使用 `np.vdot` 计算它们的向量化内积。`np.vdot` 会将矩阵元素展平成一维向量，然后执行逐元素的内积计算。\n",
    "\n",
    "矩阵 $A$ 和 $B$ 展开后为：\n",
    "\n",
    "$$\n",
    "A = \\begin{bmatrix} 1 & 2 & 3 & 4 \\end{bmatrix}, \\quad B = \\begin{bmatrix} 3 & 4 & 5 & 6 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "因此，内积的计算为：\n",
    "\n",
    "$$\n",
    "1 \\cdot 3 + 2 \\cdot 4 + 3 \\cdot 5 + 4 \\cdot 6 = 3 + 8 + 15 + 24 = 50\n",
    "$$\n",
    "\n",
    "代码使用 `np.vdot` 执行此向量化的内积计算，而不仅是逐元素矩阵乘法。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "004f7421-698b-4eb3-9d22-1517683ab9bc",
   "metadata": {},
   "source": [
    "## 导入所需库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0b0c3a1-f413-487e-9f5f-9d8a1a37248f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np  # 导入NumPy库，用于数值计算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f3ab1198-85f2-47d5-9425-46145141e92e",
   "metadata": {},
   "source": [
    "## 定义两个矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "138d24b7-39e5-4ccc-9d5c-f554db5c2eb9",
   "metadata": {},
   "outputs": [],
   "source": [
    "A = np.array([[1, 2],  # 定义矩阵A\n",
    "              [3, 4]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c79531e6-89d7-4c4c-91ae-afe500cec8bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "B = np.array([[3, 4],  # 定义矩阵B\n",
    "              [5, 6]])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "438b8825-fda5-4905-890a-6f690b7c11dd",
   "metadata": {},
   "source": [
    "## 计算矩阵的向量内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8505b1bc-d105-424b-a163-25205e0f5e94",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A_dot_B = np.vdot(A, B)  # 使用np.vdot计算A和B的向量内积\n",
    "A_dot_B\n",
    "# [1,2,3,4]*[3,4,5,6].T  # 向量化的点积计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
